package com.joca.scheduling.pages;

import java.util.Date;

import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;

import com.joca.scheduling.database.ScheduleData;
import com.joca.scheduling.entities.Role;
import com.joca.scheduling.entities.Schedule;
import com.joca.scheduling.entities.User;
import com.joca.scheduling.hibernateservices.UserServiceHibernate;

public class Reply {

	@SessionState(create = false)
	@Property
	private User user;
	private boolean userExists;
	@Property
	private String reason;
	@Property
	private Date scheduleDate;
	@Property
	private boolean reject;

	Object onActivate() {
		scheduleDate = new Date();
		if (!userExists)
			return Index.class;
		if (user.getRole() == Role.Professor) {
			return null;
		}
		return Index.class;
	}

	@Parameter
	private int scheduleID;
	@Property
	private Schedule schedule;

	Object onActivate(int scheduleID) {
		if (!userExists)
			return Index.class;
		else if (user.getRole() == Role.Professor) {
			this.scheduleID = scheduleID;
			schedule = UserServiceHibernate.getUserServiceHibernate()
					.getScheduleByID(scheduleID);// ScheduleData.getInstance().getScheduleByID(scheduleID);
			return null;
		}
		return Index.class;
	}

	int onPassivate() {
		return scheduleID;
	}

	@OnEvent
	Object onFormSubmith() {
		schedule.setReason(reason);
		schedule.setScheduleDate(scheduleDate);
		schedule.setReject(reject);
		return Professor.class;
	}

	public String getNames() {
		User u = UserServiceHibernate.getUserServiceHibernate().getUserByID(
				schedule.getScheduleRequest());
		return u.getName();

	}

	public String getSurnames() {
		User u = UserServiceHibernate.getUserServiceHibernate().getUserByID(
				schedule.getScheduleRequest());
		return u.getSurname();
	}
}
